package com.xunshibao.tutor.user.dao;

import org.springframework.stereotype.Repository;

import com.xunshibao.core.dao.BaseDAO;
import com.xunshibao.core.page.Pagination;
import com.xunshibao.tutor.po.TutorInfo;
import com.xunshibao.tutor.po.TutorInfo.STATUS;
import com.xunshibao.tutor.po.ext.TutorInfoExt;

@Repository
public class TutorInfoDaoImpl {
    private static final String INSERT = "insert into w_tinfo(tid, subid,times,school,tp,way,status,pdt,ludt,ip,area,schedule) values(?,?,?,?,?,?,?,?,?,?,?,?)";
    private static final String GET_USER_INFO = "select id, tid, subid,times,school,tp,way,status,pdt,ludt,ip,area,schedule from w_tinfo where id=? and tid=?";
    private static final String GET_BY_ID = "select i.id, i.tid, i.subid,i.times,i.school,i.tp,i.way,i.status,i.pdt,i.ludt,i.ip,i.area,i.schedule,s.name as schoolName, t.uname from w_tinfo i left join c_school s on s.tp=i.tp and s.code=i.school left join w_tutor t on t.id=i.tid where i.id=?";
    private static final String LIST = "select i.id, i.schedule, i.tid, i.subid, i.times, i.school, i.area, i.way, i.ludt, t.uname,t.gender, s.name as schoolName,i.tp from w_tinfo i LEFT JOIN w_tutor t ON i.tid = t.id LEFT JOIN c_school s ON s.code = i.school AND s.tp = i.tp WHERE i.status=? and s.tp=i.tp ORDER BY ludt DESC";
    
    public int save(TutorInfo info) {
        return BaseDAO.execute(INSERT, new Object[]{info.getTid(),info.getSubid(),info.getTimes(),info.getSchool(), info.getTp(), info.getWay(), info.getStatus(), info.getPdt(), info.getLudt(), info.getIp(), info.getArea(), info.getSchedule()});
    }
    
    public int update(String[] properties, Object[] values, String condition) {
        return BaseDAO.update(TutorInfo.TB, properties, values, condition);
    }
    
    public TutorInfo userInfo(Long infoId, Long userId) {
        return BaseDAO.queryForObject(TutorInfo.class, GET_USER_INFO, new Object[]{infoId, userId});
    }
    
    public TutorInfoExt getById(Long infoId) {
        return BaseDAO.queryForObject(TutorInfoExt.class, GET_BY_ID, new Object[]{infoId});
    }
    
    public Pagination list(STATUS status, Integer pageNo, Integer pageSize) {
        return BaseDAO.page(TutorInfoExt.class, LIST, new Object[] {status.value()}, pageNo, pageSize);
    }
}
